<style>
    .model-form .layui-form-label {
        width: 100px;
    }
    .model-form .layui-input-block {
        margin-left: 130px;
    }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">账号：</label>
                        <div class="layui-input-inline mr0">
                            <input name="account" class="layui-input" type="text" placeholder="请输入账号"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">日期：</label>
                        <div class="layui-input-inline mr0">
                            <input name="dateRange" class="layui-input date-icon" type="text" placeholder="请选择日期范围"
                                   autocomplete="off"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="versionInfoBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="versionInfoBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="versionInfoTable" lay-filter="versionInfoTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="versionInfoTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="versionInfoForm">
    <form lay-filter="versionInfoForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">版本类型</label>
            <div class="layui-input-block">
                <select name="type" lay-filter="type" lay-verify="required" required>
                    <option value="android">Android</option>
                    <option value="ios">IOS</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">版本名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入版本名称" type="text" class="layui-input"
                       maxlength="20" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">版本编号</label>
            <div class="layui-input-block">
                <input name="number" placeholder="请输入版本编号" type="text" class="layui-input"
                       maxlength="20" lay-verify="required" required/>
            </div>
        </div>
        <input name="url" type="hidden"/>
        <div class="layui-form-item" id="android" style="display: none;">
            <label class="layui-form-label">安卓安装包</label>
            <div id="btnUpload1" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传文件</div>
            <div id="btnUpload1_path" style="margin-left: 260px;line-height: 40px;"></div>
        </div>
        <div class="layui-form-item" id="ios" style="display: none;">
            <label class="layui-form-label">苹果安装包</label>
            <div id="btnUpload2" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传文件</div>
            <div id="btnUpload2_path" style="margin-left: 260px;line-height: 40px;"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">版本更新信息</label>
            <div class="layui-input-block">
                <textarea id="info" name="info" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="versionInfoFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate', 'admin', 'layedit', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var config = layui.config;
        var admin = layui.admin;
        var layedit = layui.layedit;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#versionInfoTable',
            url: config.base + 'version/admin/versionInfo/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {field: 'type', sort: true, title: '版本类型'},
                {field: 'name', sort: true, title: '版本名称'},
                {field: 'number', sort: true, title: '版本编号'},
                {field: 'url', sort: true, title: '安装文件路径'},
                {field: 'createTime', sort: true, title: '创建时间'},
                {align: 'center', toolbar: '#versionInfoTableBar', title: '操作', width: 200}
            ]]
        });

        // 工具条点击事件
        table.on('tool(versionInfoTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        // 时间范围
        laydate.render({
            elem: 'input[name="dateRange"]',
            type: 'date',
            range: true
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            if (data.field.dateRange) {
                var searchDate = data.field.dateRange.split(' - ');
                data.field.startDate = searchDate[0];
                data.field.endDate = searchDate[1];
            }
            data.field.dateRange = undefined;
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 添加按钮点击事件
        $('#versionInfoBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '1000px',
                offset: '65px',
                title: data ? '修改版本信息' : '添加版本信息',
                content: $('#versionInfoForm').html(),
                success: function () {

                    $("#android").fadeIn();
                    $("#ios").fadeOut();

                    form.on('select(type)', function(data){
                        if(data.value == "android"){
                            $("#android").fadeIn();
                            $("#ios").fadeOut();
                        }else if(data.value == "ios"){
                            $("#ios").fadeIn();
                            $("#android").fadeOut();
                        }
                    });

                    upload.render({
                        elem: '#btnUpload1'
                        ,url: config.upload_url + 'upload?path='+img_path
                        ,data: {access_token : config.getToken()}
                        ,accept: 'file'
                        ,exts: 'apk|ipa'
                        ,done: function(res){
                            if(res.code == 200){
                                layer.msg(res.msg, {icon: 1});
                                $('#btnUpload1_path').html(res.data.url);
                                $('input[name="url"]').val(res.data.url);
                            }else if(res.code == 502){
                                return location.replace('admin/login.html');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    upload.render({
                        elem: '#btnUpload2'
                        ,url: config.upload_url + 'upload?path='+img_path
                        ,data: {access_token : config.getToken()}
                        ,accept: 'file'
                        ,exts: 'ipa'
                        ,done: function(res){
                            if(res.code == 200){
                                layer.msg(res.msg, {icon: 1});
                                $('#btnUpload2_path').html(res.data.url);
                                $('input[name="url"]').val(res.data.url);
                            }else if(res.code == 502){
                                return location.replace('admin/login.html');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    if(data && data.androidUrl){
                        $('#btnUpload1_path').html(data.androidUrl);
                    }

                    if(data && data.iosUrl){
                        $('#btnUpload2_path').html(data.iosUrl);
                    }

                    var layeditIndex = layedit.build('info',{
                        uploadImage: {
                            url: config.upload_url + 'layuiUpload?access_token=' +config.getToken(),
                            type: 'post'
                        }
                    });
                    form.val('versionInfoForm', data);
                    // 表单提交事件
                    form.on('submit(versionInfoFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.info = layedit.getContent(layeditIndex);
                        admin.req(data ? 'version/admin/versionInfo/update' : 'version/admin/versionInfo/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('versionInfoTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('version/admin/versionInfo/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#versionInfoBtnExport').click(function () {
            var checkRows = table.checkStatus('versionInfoTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>